<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Dell
  Date: 2018/12/19
  Time: 17:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新增角色信息</title>
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css"  media="all">
    <script language="javascript" type="text/javascript" src="My97DatePicker/WdatePicker.js"></script>
</head>
<body>


<br>
<form id="roleForm" name="roleForm" class="layui-form" action="" method="post">

    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-inline">
            <input type="text" name="name" lay-verify="name" autocomplete="off" placeholder="" class="layui-input layui-form-danger" value="">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">角色描述</label>
        <div class="layui-input-block">
            <textarea name="desc" lay-verify="desc" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>


    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">拥有权限</label>
        <div class="layui-input-block">
            <textarea name="rool" lay-verify="rool" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">拥有菜单</label>
        <div class="layui-input-block">
            <c:forEach var="l" items="${requestScope.menus}">
                <c:if test="${l.meHigh==100}" var="result" scope="session">
                    <br>
                    <input type="checkbox" id="${l.meId}" name="${l.meName}" title="${l.meName}" value="${l.meId}" lay-filter="sup">
                    <br>
                </c:if>
                <c:forEach var="k" items="${requestScope.menus}">
                    <c:if test="${k.meHigh==l.meId}">
                        <input type="checkbox" id="${k.meId}" name="${k.meName}" title="${k.meName}" value="${k.meId}" lay-filter="sub">
                    </c:if>
                </c:forEach>
            </c:forEach>
        </div>
    </div>




    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="demo1">添加</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>


</body>
<script src="../../js/jquery.min.3.1.0.js"></script>
<script src="../../layuiadmin/layui/layui.js" charset="utf-8"></script>

<script>
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({

            name: [
                /^[\u4e00-\u9fa5]{3,}$/
                ,'姓名必须为3位以上的中文字符'
            ]

            ,desc:function (value) {
                if(value==""){
                    return '内容不能为空！';
                }
            }
            ,rool:function (value) {
                if(value==""){
                    return '内容不能为空！';
                }
            }
        });


        //监听提交
        form.on('submit(demo1)', function(data){
            var index = parent.layer.getFrameIndex(window.name);

            $.ajax({
                url:"/RoleAddServlet",   //请求路径
                datatype:"json",//返回的格式
                data:$("#roleForm").serialize(), //参数
                type:"post", //什么请求
                success:function (data) { //请求成功以后
                    //AJAX成功以后返回的
                    var obj = eval('(' + data + ')');
                    layer.msg(obj.msg);

                    //延时500ms关闭窗口
                    setTimeout(function () {
                        parent.layer.close(index);
                    }, 500);
                }
            });


            return false;
        });

        //监听父级菜单选择
        form.on('checkbox(sup)', function(data){
            var json = eval('(' + '${requestScope.jsonmenus}'  + ')');
            var checkValue=data.elem.checked;
            var id=data.elem.value;
            if(checkValue){
                for(var i=0;i<json.menus.length;i++){
                    if(json.menus[i].meHigh==id){
                        $("#"+json.menus[i].meId+"").prop("checked",true);
                    }
                }
                //执行逻辑处理结束重新渲染
                layui.form.render('checkbox');
            }else {
                for(var i=0;i<json.menus.length;i++){
                    if(json.menus[i].meHigh==id){
                        $("#"+json.menus[i].meId+"").prop("checked",false);
                    }
                }
                //执行逻辑处理结束重新渲染
                layui.form.render('checkbox');
            }

        });
        //监听子级菜单选择
        form.on('checkbox(sub)',function (data) {
            var json = eval('(' + '${requestScope.jsonmenus}'  + ')');
            var checkValue=data.elem.checked;
            var id=data.elem.value;
            if(checkValue){
                for(var i=0;i<json.menus.length;i++){
                    if(json.menus[i].meId==id){
                        $("#"+json.menus[i].meHigh+"").prop("checked",true);
                    }
                }
                //执行逻辑处理结束重新渲染
                layui.form.render('checkbox');
            }
        });

    });
</script>
</html>
